﻿#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using nHydrate.Generator.Common.GeneratorFramework;
using nHydrate.Generator.Common.Util;

namespace nHydrate.Generator.Common
{
	[Serializable]
	public class ModelCacheFile
	{
		#region Class Members

		private readonly List<string> _excludeList = new List<string>();
		private int _generatedVersion = 0;
		private readonly List<string> _generatedModuleList = new List<string>();
		private string _modelFileName = string.Empty;

		#endregion

		#region Constructor

		public ModelCacheFile(IGenerator root)
		{
			if (root == null)
				throw new Exception("The root element cannot be null");

			this.IncludeGenVersion = true;

			_modelFileName = root.FileName;
			this.Load();
		}

		public ModelCacheFile(string modelFileName)
		{
			this.IncludeGenVersion = true;

			_modelFileName = modelFileName;
			this.Load();
		}

		#endregion

		#region Properties

		public bool IncludeGenVersion { get; set; }

		public List<string> ExcludeList
		{
			get { return _excludeList; }
		}

		public List<string> GeneratedModuleList
		{
			get { return _generatedModuleList; }
		}

		public int GeneratedVersion
		{
			get { return _generatedVersion; }
			set { _generatedVersion = value; }
		}

		public string FileName
		{
			get
			{
				var fileName = _modelFileName;

				//if the file has never been saved, then there is no filename
				if (string.IsNullOrEmpty(fileName)) return string.Empty;

				var fi = new System.IO.FileInfo(fileName);
				if (fi.Exists)
				{
					//Get file name
					var name = fi.Name + ".info";
					fileName = System.IO.Path.Combine(fi.DirectoryName, name);
					return fileName;
				}
				else return string.Empty;
			}
		}

		public string TableFacadeSettings { get; set; }
		public string CodeFacadeSettings { get; set; }
		public int LastLoadTime { get; set; }

		#endregion

		#region Methods

		public bool FileExists()
		{
			return File.Exists(this.FileName);
		}

		public void Save()
		{
			if (this.FileName == string.Empty) return;
			var document = new XmlDocument();
			document.LoadXml("<configuration></configuration>");

			if (this.IncludeGenVersion)
			{
				//Save GeneratedVersion
				XmlHelper.AddAttribute(document.DocumentElement, "GeneratedVersion", this.GeneratedVersion.ToString());
			}

			//Save ExcludeList
			var exludeListNode = XmlHelper.AddElement(document.DocumentElement, "excludeList");
			foreach (var key in this.ExcludeList)
			{
				XmlHelper.AddElement((XmlElement)exludeListNode, "item", key);
			}

			//Save GeneratedModuleList
			var generatedModuleListNode = XmlHelper.AddElement(document.DocumentElement, "generatedModuleList");
			foreach (var key in this.GeneratedModuleList)
			{
				XmlHelper.AddElement((XmlElement)generatedModuleListNode, "item", key);
			}

			//Table facacde
			var tFacadeNode = XmlHelper.AddElement(document.DocumentElement, "tableFacadeSettings", this.TableFacadeSettings);

			//Column facacde
			var cFacadeNode = XmlHelper.AddElement(document.DocumentElement, "columnFacadeSettings", this.CodeFacadeSettings);

			XmlHelper.AddElement(document.DocumentElement, "lastLoadTime", this.LastLoadTime.ToString());

			document.Save(this.FileName);
		}

		public void Load()
		{
			var document = new XmlDocument();
			if (!File.Exists(this.FileName)) return;
			try
			{
				document.Load(this.FileName);
			}
			catch (Exception ex)
			{
				//Not a valid XML file
				return;
			}

			//Get GeneratedVersion
			_generatedVersion = int.Parse(XmlHelper.GetAttributeValue(document.DocumentElement, "GeneratedVersion", _generatedVersion.ToString()));

			//Get ExcludeList
			this.ExcludeList.Clear();
			XmlNode exludeListNode = XmlHelper.GetElement(document.DocumentElement, "excludeList");
			if (exludeListNode != null)
			{
				foreach (XmlNode node in exludeListNode.ChildNodes)
				{
					this.ExcludeList.Add(node.InnerText);
				}
			}

			//Get ExcludeList
			this.GeneratedModuleList.Clear();
			XmlNode generatedModuleListNode = XmlHelper.GetElement(document.DocumentElement, "generatedModuleList");
			if (generatedModuleListNode != null)
			{
				foreach (XmlNode node in generatedModuleListNode.ChildNodes)
				{
					this.GeneratedModuleList.Add(node.InnerText);
				}
			}

			this.TableFacadeSettings = XmlHelper.GetNodeValue(document.DocumentElement, "tableFacadeSettings", string.Empty);
			this.CodeFacadeSettings = XmlHelper.GetNodeValue(document.DocumentElement, "columnFacadeSettings", string.Empty);
			this.LastLoadTime = int.Parse(XmlHelper.GetNodeValue(document.DocumentElement, "lastLoadTime", this.LastLoadTime.ToString()));
			XmlHelper.AddElement(document.DocumentElement, "lastLoadTime", this.LastLoadTime.ToString());
		}

		#endregion

	}
}